package com.yjc.springcloud.alibaba.service.impl;

import com.yjc.springcloud.alibaba.domain.Order;
import com.yjc.springcloud.alibaba.mapper.OrderMapper;
import com.yjc.springcloud.alibaba.service.AccountService;
import com.yjc.springcloud.alibaba.service.OrderService;
import com.yjc.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shkstart
 * @create 2022-08-17 21:39
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AccountService accountService;
    @Autowired
    StorageService storageService;

    @Override
    @GlobalTransactional(name = "yjc-create-order", rollbackFor = Exception.class)
    public void createOrder(Order order) {
        log.info("开始创建订单~~~");

////////////////// 订单 ///////////////////////////////////
        log.info("创建订单中...");
        orderMapper.createOrder(order);
        log.info("创建订单结束，创建订单成功~~~");

//////////////////// 库存 /////////////////////////////////

        log.info("扣除库存中...");
        storageService.decreaseStorage(order.getProductId(), order.getCount());
        log.info("扣除库存结束，扣除成功~~~");

//////////////////// 余额 /////////////////////////////////
        log.info("扣除余额中...");
        accountService.decreaseAccount(order.getUserId(), order.getMoney());
        log.info("扣除余额结束，扣除成功~~~");

//////////////////// 更改订单状态 /////////////////////////////////
        log.info("更改订单状态中...");
        orderMapper.updateOrder(order.getUserId(), 0);
        log.info("更改订单状态结束，更改订单状态成功~~~");


    }
}
